package com.zhonghan.recharge.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.dao.OilCustomerReleaseDao;
import com.zhonghan.recharge.pojo.OilCustomerOrder;
import com.zhonghan.recharge.pojo.OilCustomerRelease;
import com.zhonghan.recharge.service.OilCustomerOrderService;
import com.zhonghan.recharge.service.ZhFillCardService;

@Component
public class CashBackTask {
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private OilCustomerOrderService oilCustomerOrderService;
	
	@Autowired
	private OilCustomerReleaseDao oilCustomerReleaseDao;
	
	@Autowired
	private ZhFillCardService zhFillCardService;
	
	/**
	 * 每晚2点执行
	 * @throws Exception 
	 */
	public void cashBackTimedTask() throws Exception{
		logger.info("开始执行定时任务...");
		
		
		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sdf.format(today);
		
		OilCustomerRelease obj = new OilCustomerRelease();
		obj.setReleaseTime(sdf.parse(format));
		
		List<OilCustomerRelease> oilCustomerReleaseList = oilCustomerReleaseDao.selectOilCustomerReleaseByCondition(obj );
		
		// 更新状态
		for (OilCustomerRelease oilCustomerRelease : oilCustomerReleaseList) {
			
			// 根据传入条件去查询该订单是否已经开始释放
			Integer customerOrderId = oilCustomerRelease.getCustomerOrderId();
			OilCustomerOrder order = new OilCustomerOrder();
			order.setIsRelease(1L);
			order.setId(customerOrderId);
			// 查询到所有符合该条件的订单
			List<OilCustomerOrder> oilCustomerOrderList = oilCustomerOrderService.selectOilCustomerOrderByObj(order);
			// 如果存在该订单,就执行释放油卡
			if(oilCustomerOrderList.size() > 0){
				// 更新这个释放记录为已释放
				oilCustomerRelease.setIsRelease(1L);
				oilCustomerReleaseDao.updateNonEmptyOilCustomerReleaseById(oilCustomerRelease );
				
				logger.info("开始释放....");
				logger.info("释放的客户订单是:"+oilCustomerRelease.getCustomerOrderId()+" ,释放的卡有:"+oilCustomerRelease.getCardPart100()+";"+oilCustomerRelease.getCardPart1000()+";"+oilCustomerRelease.getCardPart500());
				
				String cardPart100 = oilCustomerRelease.getCardPart100();
				String cardPart500 = oilCustomerRelease.getCardPart500();
				String cardPart1000 = oilCustomerRelease.getCardPart1000();
				
				// 激活油卡(更改状态)
				if(StringUtils.isNotEmpty(cardPart100)){
					String[] card100 = cardPart100.split("-");
					zhFillCardService.updateZhFillCardByStatus(card100[0], card100[1]);
				}
				
				if(StringUtils.isNotEmpty(cardPart500)){
					String[] card500 = cardPart500.split("-");
					zhFillCardService.updateZhFillCardByStatus(card500[0], card500[1]);
				}
				if(StringUtils.isNotEmpty(cardPart1000)){
					String[] card1000 = cardPart1000.split("-");
					zhFillCardService.updateZhFillCardByStatus(card1000[0], card1000[1]);
				}
				
				//把该释放的总金额 添加到  客户订单表中的 释放金额  累加
				OilCustomerOrder oilCustomerOrder = oilCustomerOrderList.get(0);
				
				Double orderReleaseAmount = oilCustomerOrder.getReleaseAmount();
				
				Double oilRreleaseAmount = oilCustomerRelease.getReleaseAmount();
				
				orderReleaseAmount = orderReleaseAmount == null ? 0L : orderReleaseAmount;
				
				oilCustomerOrder.setReleaseAmount(orderReleaseAmount + oilRreleaseAmount);
				
				oilCustomerOrderService.updateNonEmptyOilCustomerOrderById(oilCustomerOrder);
				
				
			}
			
		}
	}
	
	
}
